package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.List;

import com.openbravo.bean.erp.viewbean.CustomerCategoryAnalysisInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.StaticSentence;

import cn.ekuma.data.dao.ViewDAO;

public class CustomerCategoryAnalysisInfoDAO extends
		ViewDAO<CustomerCategoryAnalysisInfo> {

	public CustomerCategoryAnalysisInfoDAO(I_Session s) {
		super(s);
	}

	@Override
	public CustomerCategoryAnalysisInfo readValues(DataRead dr,CustomerCategoryAnalysisInfo obj)
			throws BasicException {
		if(obj==null)
			obj=new CustomerCategoryAnalysisInfo();
		obj.setCategoryId(dr.getString(1));
		obj.setCategoryName(dr.getString(2));
		obj.setCategoryParentId(dr.getString(3));
		obj.setDebt(dr.getDouble(4));
		if(obj.getCategoryId()==null)
			obj.setCategoryId("999999");
		return obj;
	}

	@Override
	public Class getSuportClass() {
		return CustomerCategoryAnalysisInfo.class;
	}

	@Override
	public List<CustomerCategoryAnalysisInfo> list(Object filter)
			throws BasicException {
		return new StaticSentence(s, new QBFBuilder(
                "SELECT C.ID,C.NAME,C.PARENTID,SUM(S.CURDEBT) "
                + "FROM CUSTOMERS S "
                + "LEFT JOIN CUSTOMERCATEGORIES C ON S.CATEGORY = C.ID "
                + "LEFT OUTER JOIN OPERATION_CUSTOMER OP ON S.ID = OP.CUSTOMER  "
                + "LEFT OUTER JOIN OPERATION_CUSTOMER_CATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
                + "WHERE ?(QBF_FILTER) "
                + "GROUP BY C.ID,C.NAME,C.PARENTID ",
                new String[]{"OPC.ID"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.STRING}), this).list(filter);
	}
	
}
